<template>
  <div class="app-container">
    <el-form
      label-position="left"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="公告类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择公告类型"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="公告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布者" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="publisherContact">
        <el-input
          v-model="queryParams.publisherContact"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="日期"
        ><el-date-picker
          v-model="queryParams.searchTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          align="center"
          @keyup.enter.native="handleFuzzyQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleFuzzyQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:notice:remove']"
          >删除</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:notice:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公告类型" align="center" prop="type" />
      <el-table-column
        label="公告标题"
        align="center"
        prop="title"
        show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="发布者"
        align="center"
        prop="publisher"
        width="100px"
      /> -->
      <el-table-column
        label="发布日期"
        align="center"
        prop="createTime"
        width="180px"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关键词" align="center" prop="keyword" />
      <el-table-column
        label="内容预览"
        align="center"
        prop="content"
        show-overflow-tooltip="true"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetail(scope.row)"
            v-hasPermi="['system:notice:query']"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公告类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择公告类型" clearable>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键词" />
        </el-form-item>
        <el-form-item label="内容">
          <!-- <editor v-model="form.content" :min-height="192" /> -->
          <el-input
            v-model="form.content"
            placeholder="请输入内容"
            type="textarea"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 公告详情对话框 -->
    <el-dialog
      title="详情信息"
      :visible.sync="openDetail"
      width="80%"
      append-to-body
    >
      <el-table
        :data="currentNotice"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column label="公告类型" align="center" prop="type" />
        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="发布者" align="center" prop="publisher" />
        <el-table-column
          label="联系方式"
          align="center"
          prop="publisherContact"
        />
        <!-- <el-table-column label="关键词" align="center" prop="keyword" /> -->
        <el-table-column label="内容" align="center" prop="content" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
  fuzzyListNotice,
} from "@/api/system/notice";
import user from "@/store/modules/user";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 当前行详情
      currentNotice: [],
      //  选中行的publisherId
      publisherIds: [],
      // 弹出层标题
      title: "",
      // 是否显示新增修改弹出层
      open: false,
      // 是否展示详情记录
      openDetail: false,

      // 公告类型参数
      options: [
        {
          label: "回收公告",
          value: 0,
        },
        {
          label: "其他公告",
          value: 1,
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeId: null,
        title: null,
        publisherId: null,
        publisher: null,
        publisherContact: null,
        content: null,
        keyword: null,
        type: null,
        status: null,
        searchTime: [],
        beginDate: null,
        endDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // publisherId: [
        //   { required: true, message: "发布者id不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then((response) => {
        this.noticeList = response.rows;
        this.noticeList.forEach((x) => {
          if (x.type == 0) {
            x.type = "回收公告";
          } else {
            x.type = "其他公告";
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 按条件模糊搜索公告列表 */
    getFuzzyList() {
      console.log(this.queryParams);
      this.loading = true;
      fuzzyListNotice(this.queryParams).then((response) => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: null,
        title: null,
        publisherId: null,
        publisher: null,
        publisherContact: null,
        content: null,
        keyword: null,
        type: null,
        status: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        createBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 模糊搜索按钮操作 */
    handleFuzzyQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.searchTime.length != 0) {
        this.queryParams.beginDate = this.queryParams.searchTime[0];
        this.queryParams.endDate = this.queryParams.searchTime[1];
      }
      this.getFuzzyList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeId);
      this.publisherIds = selection.map((item) => item.publisherId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (user.state.userId != this.publisherIds[0]) {
        this.$alert("只能修改自己发布的公告！", "警告", {
          confirmButtonText: "确认",
        });
        return;
      }
      this.reset();
      const noticeId = row.noticeId || this.ids;
      getNotice(noticeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公告";
      });
    },
    /** 查看详情按钮 */
    viewDetail(row) {
      this.openDetail = true;
      this.currentNotice[0] = row;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.noticeId != null) {
            this.form.publisherId = user.state.userId;
            updateNotice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.publisherId = user.state.userId;
            addNotice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      if (
        this.publisherIds.every((item) => {
          return item == user.state.userId;
        })
      ) {
        const noticeIds = this.ids;
        this.$modal
          .confirm("是否确认删除选中公告")
          .then(function () {
            return delNotice(noticeIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      } else {
        this.$alert("只能删除自己发布的公告！", "警告", {
          confirmButtonText: "确认",
        });
        return;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/notice/export",
        {
          ...this.queryParams,
        },
        `notice_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
